const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")
const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}

const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 4,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        fixedHp: 4780,
        fixedAtt:311,
        attRate: 46.6,
        dmgRate: 46.6,
        criticalStrike: 62.2,
    },
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 15, admg:40, a2dmg: 40}, // 
        
        
    ]
}

let charaters = {
    units : {
        name: "散兵", 
        reaction: 0,
        dmgZhi: 0,
        charging: 100,
        defRate: 0,
        defence: 760,
        weapontype: "magic", // sword sword2 qiang magic bow
        hp:12491,
        base: 318.11,    
        criticalRate: 5,    
        criticalStrike: 88.4,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

let tmp = {}
CONFIG.clips_initial.forEach(item => {
    item.forEach(item2 => {
        if(tmp[item2]) {
            tmp[item2] += 1
        } else {
            tmp[item2] = 1
        }
    })
})

/*
    散兵
    lv10  普攻 1.577 1.375 1.026+1.026    重击 2.377
    lv13  e 2.023  a1dmg:77.8 a2dmg:62.2  
    lv13  q 3.128 * 5     15s/60能
    天赋
    e触发转化效果 水20空居力 火30攻击 冰20暴击 雷普攻重击时回0.6能量
    e后普攻重击时16%概率触发  冲刺不消耗体力 发射4发35%倍率风矢 概率每次未触发时叠加12%(综合大约是1/3概率) 

    命座
    1 普攻重击攻速+10% 风矢倍率+25%
    2 q增伤200%                    25.5%(比0命提升)
    3 e + 3
    4 天赋转化两个效果              41.37%(比0命提升)
    5 q + 3
    6 普攻带40%倍率 普攻回空居力    67.72%(比0命提升)
*/

let cid = "units"
let count_kit = 0;
let combination = getCombination(Array(5*CONFIG.chance + 1).fill().map((_,i)=>i), CONFIG.chance, Object.entries(tmp))
let clips_initial_data = calc_clips_initial()
let fs = require("fs");

let conns = 6 // ming zuo

calc(combination);
function calc(clipSecondData) {
    let charactersDmg = [];
    clipSecondData = clipSecondData.map(clipSecondDataItem => {
        let clipSecond = {}
        clipSecond.des = clipSecondDataItem;
        clipSecond.data = {} 
        for (let secondKey in clipSecond.des) {
            let count =  clipSecond.des[secondKey]
            clipSecond.data[secondKey] = count * CLIPDATA[secondKey]
        }
        return clipSecond
    })
    
    Object.values(WEAPONS).forEach(weapon => {
        if(charaters[cid].weapontype && weapon.weapontype != charaters[cid].weapontype && weapon.weapontype!="all") {return}
        charactersDmg.push(getDMG([charaters[cid],weapon,clips_initial_data,{...CONFIG.clips_main}], clipSecondData, (data,second) => {
            count_kit++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + data.fixedHp;
            if(weapon.skill) { weapon.skill(data); }

            // 天赋转化 冰
            data.criticalRate += 20
            if(conns >= 4) {
                data.attRate += 30
            }

            let a1dmgRate = 77.8;
            let a2dmgRate = 62.2;

            let a1Dmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate + a1dmgRate + (data.admg||0) },
                rate: 1.577 + (conns>=6?0.4:0)
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a2Dmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate + a1dmgRate + (data.admg||0) },
                rate: 1.375 + (conns>=6?0.4:0)
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let a3Dmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate + a1dmgRate + (data.admg||0) },
                rate: 1.026+ (conns>=6?0.4:0)
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            let azDmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate + a2dmgRate + (data.a2dmg||0) },
                rate: 2.377
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            let eDmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate + (data.edmg||0) },
                rate: 2.023
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            let fengshiDmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate},
                rate: (0.35+0.25) * 4
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);


            let qDmg = getDmg({
                data: { ...data,dmgRate:data.dmgRate + (data.qdmg||0) + (conns>=2?200:0) },
                rate: 3.128
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data:{...data, dmgZhi:0, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            let con1 = 4
            if(conns >= 1) {con1 = 4.3}
            if(data.a1Speed) {
                con1 *= (data.a1Speed/100 + 1)
            }
            // let point = (a1Dmg+a2Dmg+a3Dmg*2) * con1 + fengshiDmg*con1/3 + eDmg + qDmg * 5 + (data.phyDmg||0);
            let point = azDmg * 17 + eDmg + qDmg * 5 + (data.phyDmg||0);
            return {point, data, second, attack, dmg:{a1Dmg,a2Dmg,a3Dmg,azDmg,eDmg,qDmg}}
        }))
    })
    
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    
    let sortf = JSON.parse(fs.readFileSync("./sort.js").toString()) 
    let choose = charactersDmg[1];
    console.log(choose);
    let name = choose.data.name
    let keys = "_"+choose.data.weaponname+"_6命无拐饰金之梦毕业"
    let sortfData = {key: name+keys, point:choose.point, dmg:JSON.parse(JSON.stringify(choose.dmg))}
    if(sortfData.point && sortfData.point > 0) {
        let org = sortf.findIndex(item3 => item3.key == sortfData.key)
        if(org >= 0){
            sortf[org] = sortfData
        } else {
            sortf.push(sortfData)
        }
        sortf.sort((b,a) => a.point-b.point)
        // fs.writeFileSync("./sort.js", JSON.stringify(sortf))
    }

    fs.writeFileSync("./ret.js", "let a = " + JSON.stringify(choose))
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count_kit);
}

function getDMG(dataarr, clipSecond, getRet){
    clipSecond = clipSecond.map(second => {
        let panel = objValueAdd([...dataarr, second.data]);
        return  getRet(panel, second)
    })
    clipSecond.sort((b,a)=> a.point-b.point)
    return clipSecond[0]
}

function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
function getCombination (arr, chance, entrys) {
    let num = 5
    var r = [];
    (function f(t, a, n) {
        if (n === 0) {
            if(t.reduce((a1,b1) => a1+b1) !== chance * 5){return}
            let ret = {}
            for (let i in t) {
                if(t[i] > entrys[i][1]*chance){return}
                ret[entrys[i][0]] = t[i]
            }
            r.push(ret)
            return ;
        }
        for (var i = 0, l = a.length; i < l; i++) {
            f(t.concat(a[i]), a, n - 1);
        }
    })([], arr, num);
    return r;
}